package com.homework.ch10;

/**
 * @author yuxi
 * @version 1.0
 * @date 2025/10/11
 * @Description:
 */
public class ThreadStateDemo {
    public static void main(String[] args) throws InterruptedException {
        // 创建线程 - 此时线程处于 NEW 状态
        Thread thread = new Thread(() -> {
            try {
                // 线程启动后进入 RUNNABLE 状态
                System.out.println("线程正在执行，即将进入睡眠...");

                // 睡眠2秒，进入 TIMED_WAITING 状态
                Thread.sleep(2000);

                System.out.println("线程睡眠结束，继续执行...");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        // 查看 NEW 状态
        System.out.println("创建线程后状态: " + thread.getState());
        // 启动线程
        thread.start();
        // 短暂等待，确保线程启动
        Thread.sleep(100);
        // 查看 RUNNABLE 状态
        System.out.println("启动线程后状态: " + thread.getState());
        // 等待线程进入 TIMED_WAITING 状态
        Thread.sleep(500);
        System.out.println("线程睡眠时状态: " + thread.getState());
        // 等待线程执行完毕
        thread.join();
        // 查看 TERMINATED 状态
        System.out.println("线程执行完成后状态: " + thread.getState());
    }
}
